package top.goldenyear.porpoise.framework.web.core.generator.dialect.impl;

import cn.hutool.v7.core.text.StrUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * 字段类型映射
 *
 * @author airhead
 */
public class JdbcTypeMapping {

  private static final Map<String, String> MAPPING = new HashMap<>();

  static {
    registerMapping("[B", "byte[]");
    registerDateTypes();
  }

  private JdbcTypeMapping() {}

  public static void registerMapping(Class<?> from, Class<?> to) {
    registerMapping(from.getName(), to.getName());
  }

  public static void registerMapping(String from, String to) {
    MAPPING.put(from, to);
  }

  public static Map<String, String> getMapping() {
    return MAPPING;
  }

  /** 当只使用 date 类型来映射数据库的所有 "时间" 类型时，调用此方法 */
  public static void registerDateTypes() {
    registerMapping("java.sql.Time", "java.util.Date");
    registerMapping("java.sql.Timestamp", "java.util.Date");
  }

  static String getType(String jdbcType) {
    String registered = MAPPING.get(jdbcType);
    return StrUtil.isNotBlank(registered) ? registered : jdbcType;
  }
}
